<?php


namespace app\test\controller;
use app\common\Qiniu;
use app\common\Timer;
use app\common\validate\User as UserValidate;
use app\index\model\Member as MemberModel;
use app\member\controller\db\ClassList;
use app\member\controller\db\Image;
use app\member\controller\db\Like;
use app\member\controller\db\Member;
use think\Controller;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\facade\Log;

class Test extends Controller
{
    public function index(){
         $data = array(
             'member_email'          =>  "8747asaas53@qq.com",
             'member_name'           =>  "aisden1",
             'member_password'       =>  "Hj9812230911@",
         );
        $member = new \app\index\controller\Member();
        $mess = $member->registerThisInBase($data);
        return json($mess);
    }

    public function registerCheckName($nickname){
        try {
                /*-----名字不可用-----*/
                MemberModel::where("member_name", $nickname)->selectOrFail();
                return 'false';
            } catch (DataNotFoundException $e) {
            } catch (ModelNotFoundException $e) {
            } catch (DbException $e) {
                Log::warning( "出现异常");
            }
            return "true";
        }


        public function testValidate(){
            $data = [
                'name'      => 'admin',
                'email'     => 'thinkphp@qq.com',
                'password'  =>  'Hj9812230911@',
                ];
            $validate = new UserValidate();
            if(!$validate->check($data)){
                dump($validate->getError());
            }
        }
    public function testAddImage(){
        $upload = new Image();
        $data = [
            'image_md5'                 =>  "sdefasdcfdasfas786a87sa",
            'image_url'                 =>  "asdascxzcasddasdas",
            'image_member_name'        =>   "874735833@qq.com",
            'image_class'               =>  "girl",       //类别
            'image_show'                =>  "1",        //显示
        ];
        return $upload->addImage($data);
    }

    public function testShowUpload(){
        return $this->fetch("index/upload");
    }

    public function uploadImage(){
        // 获取表单上传文件 例如上传了001.jpg
        $files = request()->file("image");
        foreach ($files as $file){
            // 移动到框架应用根目录/resource/ 目录下
            $info = $file->move( '../public/resource');
            // 获取上传文件的hash散列值
            echo $info->md5();
//        echo $info->sha1();
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
                echo $info->getExtension();
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                echo $info->getSaveName();
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                echo $info->getFilename();
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }
    }

    public function selectImageAll(){
        $select = new Image();
        $result = $select->selectAll();
        dump($result);
    }

    public function selectClassAll(){
        $select = new ClassList();
        $result = $select->selectAllClass();
        dump($result);
    }

    public function selectImageByMd5($md5){
        $image = new Image();
        $data = $image->selectByMd5($md5);
        return json($data);
    }



    public function selectLike(){
        $likes = new Like();
        $data = $likes->selectImageByAuthor("aiden");
        var_dump($data);
    }
}